import { Entity } from "../../../../../Core/Module/Entity/Entity";
import { GetTypeOf, Type } from "../../../../../Decorator/Decorator";
import { IMActorHandler } from "../../../../Model/Server/Module/Actor/IMActorHandler";
import { ActorResponse } from "../../../../Model/Share/Module/ActorLocation/ActorResponse";
import { IActorLocationMessage } from "../../../../Model/Share/Module/ActorLocation/IActorLocationMessage";
import { IRequest } from "../../../../Model/Share/Module/Message/IMessage";
import { ActorHandleHelper } from "../Actor/ActorHandleHelper";

export abstract class AMActorLocationHandler<E extends Entity, Message extends IRequest> extends IMActorHandler implements IActorLocationMessage {

    protected abstract Run(entity: E, message: Message);

    public async Handle(entity: Entity, fromProcess: number, actorMessage: Object): Promise<any> {
        let message = actorMessage as Message;
        if (!message) {
            // Log.Error(`消息类型转换错误: ${GetTypeOf(actorMessage)} to ${GetTypeOf(Message)}`);
            return;
        }
        let e = entity as E;
        if (!e) {
            // Log.Error(`Actor类型转换错误: ${GetTypeOf(entity)} to ${GetTypeOf(T)} -- ${GetTypeOf(Message)}`);
            return;
        }

        const response = new ActorResponse();
        {
            response.RpcId = message.RpcId;
        };
        ActorHandleHelper.Reply(fromProcess, response);

        await this.Run(e, message);
    }

    public override GetRequestType(): Type {
        return GetTypeOf(this._RequestType);
    }

    public override GetResponseType(): Type {
        return GetTypeOf(ActorResponse);
    }

}